#!/bin/bash

. $TEST_ROOT_DIR/run-test-common

set -x

# This test uses a large dataset which is not part of the CNTK repository itself
# We use the dataset from an external location specified using an environment variable
if [[ "$CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY" == "" || ! -d "$CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY" ]]; then
  echo 'This test uses external data that is not part of the CNTK repository. Environment variable CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY must be set to point to the external test data location'
  exit 1
fi

if [ "$OS" == "Windows_NT" ]; then
    DataSourceDir=`cygpath -au $CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY`/Image
else
    DataSourceDir=$CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY/Image
fi

# Copy the test data to the test run directory
TestDataDir=$TEST_RUN_DIR/TestData
mkdir $TestDataDir
cp -R $DataSourceDir/MNIST/v0/Train-28x28_cntk_text.txt $TestDataDir || exit $?
cp -R $TEST_DIR/../../Text/SequenceClassification/Data/Train.ctf $TestDataDir || exit $?
cp -R $TEST_DIR/../../Simple2d/Data/SimpleDataT*_cntk_text.txt $TestDataDir || exit $?
cp -R $TEST_DIR/../../../../Examples/Speech/AN4/Data/* $TestDataDir || exit $?
cp -R $TEST_DIR/../../../UnitTests/V2LibraryTests/data/* $TestDataDir || exit $?
cp -R $TEST_DIR/../../../UnitTests/ReaderTests/Data/CNTKBinaryReader/Simple_dense.bin $TestDataDir || exit $?
cp -R $TEST_DIR/../../../UnitTests/ReaderTests/Data/CNTKBinaryReader/Simple_jagged_sequence.bin $TestDataDir || exit $?

# Train model that is used by LoadLegacyModel tests.
# Setup predefined variables $DataDir, $ConfigDir, and $OutputDir that are required by cntkrun
ConfigDir=$TEST_ROOT_DIR/../../Examples/Speech/AN4/Config
OutputDir=$ConfigDir/../Output
DataDir=$ConfigDir/../Data

[ -d $OutputDir ] || mkdir $OutputDir || exit $?
[ -d $OutputDir/Models ] && rm -rf $OutputDir/Models
DeleteModelsAfterTest=0
[ -f $ConfigDir/FeedForward.cntk ] || exit 1
cntkrun FeedForward.cntk "stderr=- command=speechTrain parallelTrain=false speechTrain=[SGD=[maxEpochs=1]]" || exit $?
cp $OutputDir/Models/cntkSpeechFF.dnn $TestDataDir || exit $?

# Set CUDA_VISIBLE_DEVICES to exclude all gpu if running on cpu device
[ "$TEST_DEVICE" == "cpu" ] && export CUDA_VISIBLE_DEVICES=-1

pushd $TestDataDir

if [ "$OS" == "Windows_NT" ]; then
  TEST_BINARY=V2LibraryTests.exe
else
  TEST_BINARY=v2librarytests
fi

# Note: Run the device selection suite first since later tests may interfere with
# device selection by freezing default device
$TEST_BIN_DIR/$TEST_BINARY --report_level=detailed --run_test=DeviceSelectionSuite
ExitCode=$?

# Next is LearnerSerializationBackcompat, which also needs to run in isolation,
# since it depends on a particular state of the counter that's used to generate UIDs.
$TEST_BIN_DIR/$TEST_BINARY --report_level=detailed --run_test=SerializationSuite/LearnerSerializationBackcompat
ExitCode2=$?
(( ExitCode2 > ExitCode )) && ExitCode=$ExitCode2

$TEST_BIN_DIR/$TEST_BINARY --report_level=detailed --run_test=!DeviceSelectionSuite --run_test=!SerializationSuite/LearnerSerializationBackcompat
ExitCode2=$?

(( ExitCode2 > ExitCode )) && ExitCode=$ExitCode2

# Delete the test data
popd
rm -rf $TestDataDir

exit $ExitCode
